Skip to content

feat: Add CLI-driven event sync with Watch and Refresh - #2679

Draft
murarustefaan wants to merge 13 commits into
mainfrom
feat/cli-driven-event-sync
Draft

murarustefaan wants to merge 13 commits into
mainfrom
feat/cli-driven-event-sync

Conversation

@murarustefaan

@murarustefaan murarustefaan commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Summary

Source plugins can implement WatchClient and Refresher, and the SDK serves the Watch RPC and refresh syncs, where a failed refresh of one resource is reported as a sync error and does not fail the call.

Depends on cloudquery/plugin-pb-go#729. go.mod requires the released plugin-pb-go, so the build fails until that pull request is released.


Use the following steps to ensure your PR is ready to be reviewed

  • Read the contribution guidelines 🧑‍🎓
  • Run go fmt to format your code 🖊
  • Lint your changes via golangci-lint run 🚨 (install golangci-lint here)
  • Update or add tests 🧪
  • Ensure the status checks below are successful ✅

murarustefaan and others added 13 commits September 18, 2026 13:53
Adds the SDK-side vocabulary for event-based syncing: Op, Change,
WatchOptions, Position, Ready, RefreshResource, the optional WatchClient
interface and SyncOptions.Refresh.

Plugin.WatchReady, Plugin.Watch and Plugin.Ack type-assert the client to
WatchClient and return ErrNotImplemented otherwise. None of them takes
Plugin.mu: a watch stream is long-lived and must not block the refresh
syncs that run alongside it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
NewKeysRecord builds the one-row record that identifies a single resource
by its primary keys, always in table.PrimaryKeys() order so the same
resource always produces the same record. KeysString renders one as
"col=val,col=val" for logging and for coalescing changes.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Builds the delete message for a single resource from its keys record: one
AND group of EQ predicates, one per primary key column, plus the relations
of every descendant table so destinations can cascade the delete.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Refresh re-fetches individual resources by their keys and runs the items
the Refresher returns through the regular resolve pipeline: column
resolvers, PostResourceResolver, relations, CQID calculation and insert
batching. It emits no migrate messages and never multiplexes, since the
caller already knows the table and the client.

OP_DELETE skips the fetch and ErrNotFound turns into a delete message. A
resource that fails is reported as a SyncError and does not stop the
others; all failures come back joined.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Watch drives the stream handshake - Start, Ready, Position, Change...,
Drained - forwards the client's Acks while changes stream out, and reports
Unimplemented for plugins that do not implement WatchClient. Sync maps
Sync.Request.refresh into SyncOptions.Refresh.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
A watch stream can sit idle between changes, so the server pings idle
connections every 30s and permits client pings without a stream. Shutdown
now lets in-flight RPCs finish, falling back to a hard stop after 30s so a
long-lived watch cannot hold the server open.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Exercises the handshake and drain, ack forwarding, the first message
having to be Start, Unimplemented without a WatchClient, and refresh syncs
producing inserts for found keys and delete records for missing ones. The
refresh-while-watching case fails if Plugin.Watch takes the plugin lock.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The example now implements plugin.WatchClient and scheduler.Refresher so an
event-based sync can be driven end to end without a cloud behind it.

Two optional spec fields: `changes` replays canned events over the Watch
stream, `refresh_data` holds the rows a refresh sync can find. WatchReady
connects to the state backend and reports the checkpoint stored under
`events:checkpoint`; Ack writes it back and flushes. Full syncs are
unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
WatchReady is called first and alone; Watch and Ack then run concurrently
from different goroutines for the lifetime of the stream, and an Ack
routinely lands while Watch is blocked. Neither the SDK nor the gRPC
server locks around them, and a refresh Sync can run on the same client at
the same time, so implementations must guard whatever the two share. Close
can also land on a live watch.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Watch read the checkpoint while Ack wrote it, and Close nil'd the state
client that Ack dereferences, all without synchronization. A mutex now
covers stateClient and checkpoint; Ack holds it for the whole write, so
Close waits for an in-flight ack instead of closing underneath it.

Adds two tests that drive Watch, Ack and Close concurrently. Both report a
data race against the previous code under -race.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ared

AWS tables keep _cq_id as their only primary key and carry resource
identity in PrimaryKeyComponent columns, which is also what
Resource.CalculateCQID hashes. Keying on PrimaryKeys() rejected every one
of them.

Table.IdentityColumns() now defines the set once - components when the
table declares any, otherwise primary keys - and NewKeysRecord,
NewDeleteRecordByKeys and the refresh delete path all use it. KeysString
is unchanged, since it just walks the record it is given.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…records

An event rarely knows a resource's whole identity: aws_iam_policies has
identity columns account_id, id, input_hash and arn, while a CloudTrail
event carries only account_id and arn. Requiring all of them rejected
those tables outright.

A keys record may now carry any non-empty subset of the identity columns,
still in the table's declaration order, and deletes predicate on exactly
the columns present. Columns outside the identity set, and empty records,
are still rejected.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant